package com.dhcc.DRGsHosp.web.controller;

import java.util.HashMap;
import java.util.Map;

import org.openkoala.koala.commons.InvokeResult;
import org.openkoala.security.shiro.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dhcc.DRGsHosp.facade.QuotaFacade;
import org.codehaus.jackson.map.ObjectMapper;

/**
 * @title		:QuotaController.java
 * @description	:指标可配置
 * @author		:lileizheng
 * @date		:2016年6月12日
 * @version     :1.0
 * @copyright   :DHCC 2013-2014 
 */
@Controller("drgsHospWebController35")
@RequestMapping("/quota")
public class QuotaController {

	@Autowired
	private QuotaFacade quotaFacade;
	
	/**
	 * 
	 *  getQuotas.  
	 *  @Description：获取页面指标配置
	 *  @Author LiuHongjun
	 *  @param pageCode
	 *  @param userCode
	 *  @param roleCode
	 *  @return 
	 *  @Date:2017年7月28日下午3:40:38
	 */
	@ResponseBody
	@RequestMapping(value = "/getQuotas")
	public InvokeResult getQuotas(String pageCode, String userCode, String roleCode){
		if(roleCode == null){
			roleCode = CurrentUser.getRoleName();
		}
		return quotaFacade.getQuotas(roleCode, CurrentUser.getUserAccount(),pageCode);
	}
	/**
	 * 
	 *  getQuotasByRole.  
	 *  @Description：根据角色获取配置的指标
	 *  @Author LiuHongjun
	 *  @param userCode
	 *  @param roleCode
	 *  @return 
	 *  @Date:2017年7月27日下午4:25:24
	 */
	@ResponseBody
	@RequestMapping(value = "/getQuotasByRole")
	public InvokeResult getQuotasByRole(String userCode, String roleCode){
		return quotaFacade.getQuotasByRole(roleCode, userCode);
	}
	/**
	 *  saveQuotas.  
	 *  @Description：页面指标配置更新
	 *  @Author LiuHongjun
	 *  @param param
	 *  @param userCode
	 *  @param roleCode
	 *  @return 
	 *  @Date:2017年7月28日下午3:32:04
	 */
	@ResponseBody
	@RequestMapping(value = "/saveQuotas")
	public InvokeResult saveQuotas(String param, String userCode, String roleCode){
		ObjectMapper objectMapper = new ObjectMapper();
		Map<String, Object> json;
		try {
			json = objectMapper.readValue(param, Map.class);
		} catch (Exception e) {
			e.printStackTrace();
			json = new HashMap<String, Object>();
		} 
		if(roleCode == null){
			roleCode = CurrentUser.getRoleName();
		}
		json.put("roleCode", roleCode);
		json.put("userCode", CurrentUser.getUserAccount());
		return quotaFacade.updatePagesQuotas(json);
	}
	/***
	 *  adminSaveQuotas.  
	 *  @Description：存储管理员下配置指标
	 *  @Author LiuHongjun
	 *  @param param
	 *  @param roleCode
	 *  @return 
	 *  @Date:2017年7月28日下午2:54:27
	 */
	@ResponseBody
	@RequestMapping(value = "/adminSaveQuotas")
	public InvokeResult adminSaveQuotas(String param, String roleCode,String orderId){
		ObjectMapper objectMapper = new ObjectMapper();
		Map<String, Object> json;
		try {
			json = objectMapper.readValue(param, Map.class);
		} catch (Exception e) {
			e.printStackTrace();
			json = new HashMap<String, Object>();
		} 
		json.put("roleCode", roleCode);
		json.put("orderId", orderId);
 		json.put("userCode", CurrentUser.getUserAccount());
		return quotaFacade.adminSaveQuotas(json);
	}
	@ResponseBody
	@RequestMapping(value = "emptyQuotaSetting")
	public InvokeResult emptyQuotaSetting(String userCode, String roleCode){
		if(userCode == null){
			userCode = CurrentUser.getUserAccount();
		}
		if(roleCode == null){
			roleCode = CurrentUser.getRoleName();
		}
		return quotaFacade.emptyQuotaSetting(userCode, roleCode);
	}
	/**
	 * 
	 *  saveQuota.  
	 *  @Description：保存指标
	 *  @Author LiuHongjun
	 *  @param param
	 *  @return 
	 *  @Date:2017年9月6日下午4:04:11
	 */
	@ResponseBody
	@RequestMapping(value = "/saveQuota")
	public InvokeResult saveQuota(String param){
		ObjectMapper objectMapper = new ObjectMapper();
		Map<String, Object> json;
		try {
			json = objectMapper.readValue(param, Map.class);
		} catch (Exception e) {
			e.printStackTrace();
			json = new HashMap<String, Object>();
		} 
		return quotaFacade.saveQuota(json);
	}
	/**
	 *  @desp  ：获取指标列表
	 *  @Author： lhj
	 *  @return
	 *  @Date  :2018年2月7日上午11:18:42
	 */
	@ResponseBody
	@RequestMapping(value = "/getQuotaList")
	public InvokeResult getQuotaList(){
		return quotaFacade.getQuotaList(null);
	}
	
	@ResponseBody
	@RequestMapping(value = "/getQuotaByCode")
	public InvokeResult getQuotaByCode(String quotaCode){
		return quotaFacade.getQuotaByCode(quotaCode);
	}
	
	@ResponseBody
	@RequestMapping(value = "/delQuota")
	public InvokeResult delQuota(String quotaCode){
		return quotaFacade.delQuota(quotaCode);
	}
	/**
	 * 
	 *  getQuotaRelease.  
	 *  @Description：指标获取详情
	 *  @Author LiuHongjun
	 *  @param quotaCode
	 *  @return 
	 *  @Date:2017年7月15日上午8:02:27
	 */
	@ResponseBody
	@RequestMapping(value = "/getQuotaRelease")
	public InvokeResult getQuotaRelease(String quotaCode){
		return quotaFacade.getQuotaRelease(quotaCode);
	}
	/**
	 * 
	 *  getQuotaReleaseList.  
	 *  @Description：获取指标详情
	 *  @Author LiuHongjun
	 *  @param quotaType
	 *  @return 
	 *  @Date:2017年7月15日上午8:03:58
	 */
	@ResponseBody
	@RequestMapping(value = "/getQuotaReleaseList")
	public InvokeResult getQuotaReleaseList(String quotaType){
		return quotaFacade.getQuotaList(quotaType);
	}
	/**
	 * 
	 *  getQuotaList.  
	 *  @Description：获取用户角色授权的添加列表
	 *  @Author LiuHongjun
	 *  @param quotaType
	 *  @return 
	 *  @Date:2017年7月27日下午5:45:46
	 */
	@ResponseBody
	@RequestMapping(value = "/getQuotaRoleList")
	public InvokeResult getQuotaRoleList(String quotaType){
		return quotaFacade.getQuotaRoleList(quotaType);
	}
	@ResponseBody
	@RequestMapping(value = "/saveQuotaRelease")
	public InvokeResult saveQuotaRelease(String param){
		ObjectMapper objectMapper = new ObjectMapper();
		Map<String, Object> json;
		try {
			json = objectMapper.readValue(param, Map.class);
		} catch (Exception e) {
			e.printStackTrace();
			json = new HashMap<String, Object>();
		} 
		return quotaFacade.saveQuotaRelease(json);
	}
	
	@ResponseBody
	@RequestMapping(value = "/delInvalidReleaseQuotas")
	public InvokeResult delInvalidReleaseQuotas(String param, String quotaCode){
		return quotaFacade.delInvalidReleaseQuotas(param, quotaCode);
	}
	
	@ResponseBody
	@RequestMapping(value = "/deleteQuotaSettingByCode")
	public InvokeResult deleteQuotaSettingByCode(String quotaCode, String roleCode,String pid,String id){
		return quotaFacade.deleteQuotaSettingByCode(quotaCode, roleCode,pid,id);
	}
	
	@ResponseBody
	@RequestMapping(value = "/clearQuotaSettings")
	public InvokeResult clearQuotaSettings(String userCode, String roleCode, String pageCode){
		if(userCode == null){
			userCode = CurrentUser.getUserAccount();
		}
		if(roleCode == null){
			roleCode = CurrentUser.getRoleName();
		}
		return quotaFacade.clearQuotaSettings(userCode, roleCode, pageCode);
	}
	/**
	 * 
	 *  allUpdateQuotaSql.  
	 *  @Description：批量更新sql
	 *  @Author LiuHongjun
	 *  @return 
	 *  @Date:2017年7月20日下午2:10:17
	 */
	@ResponseBody
	@RequestMapping(value = "/allUpdateQuotaSql")
	public InvokeResult allUpdateQuotaSql(){
		return quotaFacade.allUpdateQuotaSql();
	}
}
